Posts

Post not yet marked as solved
9 Replies
Thanks for the help.I did not need var hkHomeAccessory: HMAccessoryDelegate! var delegate: HMAccessoryDelegate? I added... oneAccessory!.delegate = selfIn earlier trials, I had tried to add a delegate to the Characteristic.Here is the code that is currently working.import Foundation import HomeKit let hkHomeManager = HomeKitManagerClass() let hkAccessoryManager = HomeKitAccessoryClass() let myCharTypes = readCharacteristicsCSV() class HomeKitAccessoryClass: NSObject, HMAccessoryDelegate { // accessoryChangedValue func accessory (_ accessory: HMAccessory, service: HMService, didUpdateValueFor characteristic: HMCharacteristic) { // When an accessory (HomeKit Device) changes a value, // this func should be called by Apple so that I can act on the changed value //print ("Do other interesting things here") let printout: String = "DidUpdateValue, \(accessory.name), \(service.name), \(characteristic.characteristicType), \(characteristic.localizedDescription), \(String(describing: characteristic.value))" //print (printout) globalCSVOut.append(printout) } func hkNotifications(idIn: String) { if !appSettings.key.homeKitAllowed { return } if let index = getDeviceIndexById(idIn) { let oneAccessory = globalDevices.deviceList[index].hkAccessory // Device = Accessory from HomeKit if oneAccessory == nil { return } // no HomeKit Accessory Found if oneAccessory!.isReachable == false && oneAccessory!.isBlocked { return } // Don't try to access this device} oneAccessory!.delegate = self print("hkNotificaitons Start \(globalDevices.deviceList[index].displayName)") for oneService in oneAccessory!.services { let hkCharacteristics = oneService.characteristics for indexCharacteristics in 0..<hkCharacteristics.count { let hkChar = hkCharacteristics[indexCharacteristics] if hkChar.isNotificationEnabled { do {} //print("hkNotificaitons are ALREADY enabled for: \(globalDevices.deviceList[index].displayName)") } else { if hkChar.characteristicType == "00000008-0000-1000-8000-0026BB765291" { // Test ONLY Brightness for now hkChar.enableNotification(true, completionHandler: { error in if error == nil { do {} //print("hkNotificaitons are set to TRUE for: \(globalDevices.deviceList[index].displayName)") } else { //print("\(oneAccessory?.name) \(hkChar.characteristicType), Error in Notification Set \(error!.localizedDescription)") } } ) } } } } } }
Post not yet marked as solved
9 Replies
Sorry for the delay. I did get it working and plan to reply tonight with details. Lots of family / holiday stuff the past week.Thanks for the help!
Post not yet marked as solved
9 Replies
Claude and friends, Here are more thoughts...Here is my summary of the thread you suggested…A protocol named UpdateVC1 is declared outside of any class.The class VC1 uses that protocol named UpdateVC1In that VC1 class, there is a function named updateName, which corresponds to the func name in the protocol UpdateVC1The class VC2 has a var named delegate with an optional type of the same protocol name UpdateVC1When VC2 wants VC1 to know about the change in value of the name, it calls it's delegate.updateName.This passes the value from VC2 to VC1Is that generally accurate?In VC2, is the var 'delegate' a reserved word in swift or could it have been:var anyOtherNameDelegate: UpdateVC1?My app is only using swiftUI and I have no experience with ViewControllers.My objective is that when the brightness value on a lamp changes from 10% to 50% I would like to be notified.I think... HomeKit has a defined protocol named HMAccessoryDelegate with a method accessory(_:service:didUpdateValueFor:)https://developer.apple.com/documentation/homekit/hmaccessorydelegatehttps://developer.apple.com/documentation/homekit/hmaccessorydelegate/1615286-accessoryIn comparing my objective to the ViewController example, I believe that Apple HomeKit has already declared the protocol with several functions.One of the declared functions is: Accessory didUpdateValueForoptional func accessory(_ accessory: HMAccessory, service: HMService, didUpdateValueFor characteristic: HMCharacteristic)Here is my most recent attempt...import Foundation import HomeKit let hkHomeManager = HomeKitManagerClass() let hkAccessoryManager = HomeKitAccessoryClass() let myCharTypes = readCharacteristicsCSV() class HomeKitAccessoryClass: NSObject, HMAccessoryDelegate { var hkHomeAccessory: HMAccessoryDelegate! var delegate: HMAccessoryDelegate? func accessory (_ accessory: HMAccessory, service: HMService, didUpdateValueFor characteristic: HMCharacteristic) { // When an accessory (HomeKit Device) changes a value, // this func should be called by Apple so that I can act on the changed value print ("Did Update Value For...") print ("Do other interesting things here") print (accessory.name) print (service.name) print (characteristic.metadata) } func hkNotifications(idIn: String) { if !appSettings.key.homeKitAllowed { return } if let index = getDeviceIndexById(idIn) { if !(globalDevices.deviceList[index].displayName).contains("Couch") { return } // Test ONLY LR Couch Light print("hkNotificaitons Start \(globalDevices.deviceList[index].displayName)") let oneDevice = globalDevices.deviceList[index].hkDevice // Device = Accessory from HomeKit if oneDevice == nil { return } // no HomeKit Accessory Found if oneDevice!.isReachable == false && oneDevice!.isBlocked { return } // Don't try to access this device} for indexServices in 0..<oneDevice!.services.count { let hkCharacteristics = oneDevice!.services[indexServices].characteristics for indexCharacteristics in 0..<hkCharacteristics.count { let hkChar = hkCharacteristics[indexCharacteristics] //if ([thisCharacteristic.properties containsObject:HMCharacteristicPropertySupportsEventNotification]) { // [thisCharacteristic enableNotification:TRUE completionHandler:^(NSError *error) { if hkChar.isNotificationEnabled { print("hkNotificaitons are ALREADY enabled for: \(globalDevices.deviceList[index].displayName)") } else { if hkChar.characteristicType.contains("0") { // == "00000008-0000-1000-8000-0026BB765291" { // Test ONLY Brightness for now hkChar.enableNotification(true, completionHandler: { error in if error != nil { print("\(hkChar), Error in Notification Sets \(error!.localizedDescription)") } else { print("hkNotificaitons are set to TRUE for: \(globalDevices.deviceList[index].displayName)") } } ) } } } } } } }And... Thanks for helping!Steve
Post not yet marked as solved
9 Replies
Claude,You are not wrong. I don't understand delegation. I will have to look at it bit to explain line 22. I 'copied' the concept from something else that appears to work.But first, I will review the link you suggested.Thanks for getting this started!Steve.
Post marked as solved
2 Replies
Issue Resolved. When I added Homekit to the Signing and Capabilities Tab, I must have had Debug selected instead of Release or All. I beieve this stopped the Home Kit functionality from running as expected when not in debug.I added HomeKit in Release and now everything is working as expected.
Post marked as solved
2 Replies
As I read this, it's a bit misleading at the end.Using HMHomeManager.homes it returns two homes when I access from xcode. When running from the testflight version it returns ZERO homes. My Auth Status is always a 5.